var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

const [hostName, port] = ['localhost',4567];

app.get('/', function(req, res){
	// res.send('<h1>Hello world</h1>');
	res.sendFile(__dirname + '/index.html');
});

io.on('connection',function(socket){
	console.log('a user connected');
	///连接时，对对方client打声招呼
	socket.broadcast.emit('hi');

	socket.on('chat message', function(msg){
		//接收到消息，打印一波
		console.log(`message:${msg}`);

		///群发
		io.emit('chat message', `${socket.id} say:${msg}`);
	});

  	socket.on('disconnect', function(){
  	  	console.log('user disconnected');
  	});
});

http.listen(port, function(){
	console.log(`listening on ${hostName}:${port}`);
});

